
foreach var in "" {
cd "$revdta"

use "Full_Panel_2b.dta", clear

keep if sample_select==1 & educ <= 2

/*Drops those who have spells of self-employment or jumps in their records*/
egen sf=sum(self==3),by(newid)				/*# of self-empl records*/
gen todrop=sf>0								
drop if todrop==1
drop todrop

drop if educ >2
keep if age >= 23& age <=72

bysort newid: egen firstage = min(age)
bysort newid: egen firsthealth = max(DS * (age==firstage))



local suffix

tokenize $typename
foreach type in $type  {
*type_prodpost

sum year
merge m:1 newid using "./intermediary/sp_temp_types"
keep if _merge == 3
if("$type" != "type_prodwage") ren type_prodwage type_prod
ren $type type_prodwage
*replace type_prodwage = 1 if type_prodwage ==. 
drop _merge
gen fsizeold=fsize
gen kidsold=kids
replace type_prodwage = 1 if age > 62
collapse (mean) fsize kids, by(age type_prodwage married)
expand 2 if age > 62 & type_prodwage == 1, gen(dup)
replace type_prodwage = 2 if dup == 1
drop dup
expand 2 if age > 62 & type_prodwage == 1, gen(dup)
replace type_prodwage = 3 if dup == 1
drop dup
gen numadults = fsize  - kids

levelsof type_prodwage, local(types)
forvalues m=0(1)1 {
foreach j of local types	{
			lowess numadults age if type_prodwage==`j'&married==`m',gen(numadults_m`m'_t`j'q) nog 
			ipolate numadults_m`m'_t`j'q age if type_prodwage==`j' &married==`m',gen(numadults_m`m'_t`j') e
			
			lowess kids age if type_prodwage==`j'&married==`m',gen(kids_m`m'_t`j'q) nog 
			ipolate kids_m`m'_t`j'q age if type_prodwage==`j' &married==`m',gen(kids_m`m'_t`j') 
					}
}
egen numadults_p = rowtotal(numadults_m?_t?)
egen kids_p = rowtotal(kids_m?_t?)

keep age married type_prodwage kids_p numadults_p

cd "$out"

export delimited using "famsize_married`suffix'`1'" if married==1, delimiter(",") replace
export delimited using "famsize_single`suffix'`1'" if married==0, delimiter(",") replace

local m = 0
twoway (connected numadults_p age if type_prodwage == 1 & married == `m') (connected numadults_p age if type_prodwage == 2 & married == `m') (connected numadults_p age if type_prodwage == 3 & married == `m'), legend(label(1 Low Type) label(2 Moderate Type) label(3 High Type)) xtitle(Age, size(vlarge)) ytitle("Adults in Household", size(vlarge)) ylabel(#6, labsize(vlarge))  xlabel(, labsize(vlarge))  plotregion(color(white)) graphregion(color(white)) name(Single, replace) title(Non-Married Workers, size(vlarge) color(black)) yscale(range(1.2 2.6))
local m = 1
twoway (connected numadults_p age if type_prodwage == 1 & married == `m') (connected numadults_p age if type_prodwage == 2 & married == `m') (connected numadults_p age if type_prodwage == 3 & married == `m'), legend(label(1 Low Type) label(2 Moderate Type) label(3 High Type)) xtitle(Age, size(vlarge)) ytitle("Adults in Household", size(vlarge)) ylabel(#6, labsize(vlarge))  xlabel(, labsize(vlarge))  plotregion(color(white)) graphregion(color(white)) name(Married, replace) title(Married Workers, size(vlarge) color(black)) yscale(range(1.2 2.6))
grc1leg Single Married, legendfrom(Married) plotregion(color(white)) graphregion(color(white))
graph export "numadults`suffix'`1'.pdf", replace


local m = 0
twoway (connected kids_p age if type_prodwage == 1 & married == `m') (connected kids_p age if type_prodwage == 2 & married == `m') (connected kids_p age if type_prodwage == 3 & married == `m'), legend(label(1 Low Type) label(2 Moderate Type) label(3 High Type)) xtitle(Age, size(vlarge)) ytitle("Adults in Household", size(vlarge)) ylabel(#6, labsize(vlarge))  xlabel(, labsize(vlarge))  plotregion(color(white)) graphregion(color(white)) name(Single, replace) title(Non-Married Workers, size(vlarge) color(black)) yscale(range(0 2))
local m = 1
twoway (connected kids_p age if type_prodwage == 1 & married == `m') (connected kids_p age if type_prodwage == 2 & married == `m') (connected kids_p age if type_prodwage == 3 & married == `m'), legend(label(1 Low Type) label(2 Moderate Type) label(3 High Type)) xtitle(Age, size(vlarge)) ytitle("Children in Household", size(vlarge)) ylabel(#6, labsize(vlarge))  xlabel(, labsize(vlarge))  plotregion(color(white)) graphregion(color(white)) name(Married, replace) title(Married Workers, size(vlarge) color(black)) yscale(range(0 2))
grc1leg Single Married, legendfrom(Married) plotregion(color(white)) graphregion(color(white))
graph export "numkids`suffix'`1'.pdf", replace

macro shift
}
}
